import 'antd-mobile/2x/es/global';
import './app.less';
import './theme.less';
import { Toast } from 'antd-mobile';
import { history } from '@umijs/max';

enum ErrorShowType {
  SILENT = 0,
  WARN_MESSAGE = 1,
  ERROR_MESSAGE = 2,
  NOTIFICATION = 3,
  REDIRECT = 9,
}


export const request = {
  timeout: 3000,
  errorConfig: {
    errorHandler: (error: any, opts: any) => {
      if (opts?.skipErrorHandler) throw error;
      // 我们的 errorThrower 抛出的错误。
      if (error.name === 'BizError') {
        const errorInfo: any = error.info;
        if (errorInfo) {
          const { errorMessage, errorCode } = errorInfo;
          switch (errorInfo.showType) {
            case ErrorShowType.SILENT:
              // do nothing
              break;
            case ErrorShowType.WARN_MESSAGE:
              Toast.show({
                icon: 'fail',
                content: errorMessage,
              })
              break;
            case ErrorShowType.ERROR_MESSAGE:
              Toast.show({
                icon: 'fail',
                content: errorMessage,
              })
              break;
            case ErrorShowType.NOTIFICATION:
              Toast.show({
                icon: 'fail',
                content: errorMessage,
              })
              break;
            case ErrorShowType.REDIRECT:
              // TODO: redirect
              break;
            default:
              Toast.show({
                icon: 'fail',
                content: errorMessage,
              })
          }
        }
      } else if (error.response) {
        // Axios 的错误
        // 请求成功发出且服务器也响应了状态码，但状态代码超出了 2xx 的范围
        Toast.show({
          icon: 'fail',
          content: `Response status:${error.response.status}`,
        })
        if (error.response.status === 401) {
          window.localStorage.removeItem('token');
          history.push('/tab-bar/personalCenter');
        }
      } else if (error.request) {
        // 请求已经成功发起，但没有收到响应
        // \`error.request\` 在浏览器中是 XMLHttpRequest 的实例，
        // 而在node.js中是 http.ClientRequest 的实例
        Toast.show({
          icon: 'fail',
          content: `None response! Please retry.`,
        })
      } else {
        // 发送请求时出了点问题
        Toast.show({
          icon: 'fail',
          content: `Request error! Please retry.`,
        })
      }
    },

    errorThrower: (res: any) => {
      const { success, data, errorCode, errorMessage, showType } = res;
      if (!success) {
        const error: any = new Error(errorMessage);
        error.name = 'BizError';
        error.info = { errorCode, errorMessage, showType, data };
        throw error; // 抛出自制的错误
      }
    },
  },
  requestInterceptors: [
    (request: any) => {
      // 不再需要异步处理读取返回体内容，可直接在data中读出，部分字段可在 config 中找到
      const { data = {} as any, config } = request;
      // do something
      return request
    },
  ],
  responseInterceptors: [
    (response: any) => {
      // 不再需要异步处理读取返回体内容，可直接在data中读出，部分字段可在 config 中找到
      const { data = {} as any, config } = response;
      console.log('response', response)
      // do something
      return response
    },
  ]
};